﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using System.Drawing;
using BenzInfo.Hockey.Grenadiers.Utils;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class PicturesController : BaseController
    {
        private const string BASE_PATH = "~/Content/images/pictures";

        private GrenadiersContext db = new GrenadiersContext();

        //
        // GET: /Pictures/

        public ViewResult Index()
        {
            return View(db.Pictures.ToList());
        }

        //
        // GET: /Pictures/Details/5

        public ViewResult Details(int id)
        {
            Picture picture = db.Pictures.Find(id);
            return View(picture);
        }

        //
        // GET: /Pictures/Create

        public ActionResult Add()
        {
            return PartialView();
        } 

        //
        // POST: /Pictures/Create

        [HttpPost]
        public ActionResult Add(Picture picture, HttpPostedFileBase inputFile)
        {
            if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {

                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length-1];
                    string filename = string.Format("{0:000}.{1}", DateTime.Now.Ticks, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    picture.FileName = filename;
                }
            }

            if (ModelState.IsValid)
            {
                db.Pictures.Attach(picture);
                db.Pictures.Add(picture);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(picture);
        }

        public ActionResult Edit(int id)
        {
            Picture picture = db.Pictures.Find(id);

            return PartialView("Edit", picture);
        }

        //
        // POST: /Contest/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(Picture picture, HttpPostedFileBase inputFile, string nopicture)
        {
            if (nopicture == "on")
            {
                picture.FileName = null;
            }
            else if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, picture.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    picture.FileName = filename;
                }
            }

            if (ModelState.IsValid)
            {
                db.Entry(picture).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(picture);
        }

        public ActionResult Delete(int id)
        {
            Picture picture = db.Pictures.Find(id);
            db.Pictures.Remove(picture);
            db.SaveChanges();

            try{
                string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), picture.FileName);

                System.IO.File.Delete(filepath);
            }
            catch (Exception) { }

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}